<?php
require_once "includes/common.php";

unset($_POST['conPassword']);
$_POST['lang'] = implode(',',$_POST['lang']);
$_POST['hobbies'] = implode(',',$_POST['hobbies']);
$_POST['register_date'] = date('Y-m-d H:i:s');
$_POST['status'] = 1;
pr($_POST);

// file copy start
if($_FILES['avatar']['error']==0) {
  $fileSizeInMb = ($_FILES['avatar']['size'])/(1024*1024);
  if($fileSizeInMb <= 1) {
    $fileName = time().rand().$_FILES['avatar']['name'];
    $source = $_FILES['avatar']['tmp_name'];
    $dest = "uploads/$fileName";

    copy($source, $dest);

    /*
     *  Code to generate thumb
     */
    require_once('thumbnail/thumb.php');
    $img->max_x        = 100;
    $img->max_y        = 100;
    $img->GenerateThumbFile($dest, "uploads/thumbs/$fileName");
//
    // $img->max_x        = 50;
    // $img->max_y        = 50;
    // $img->GenerateThumbFile($dest, "uploads/chats/$fileName");
//

    // must override with updated name of file
    // by which name we save in uploads folder
    $_POST['avatar'] = $fileName;
  } else {
    echo "<h1>You are trying to upload $fileSizeInMb MB.</h1>";
    echo "<h1>Avatar should not be more then 1 Mb</h1>";
  }
} else
if($_FILES['avatar']['error']==1) {//will execute when in php.ini (line no 921  upload_max_filesize = 2M

   echo "<h1>Avatar should not be more then 2 Mb</h1>";
} else {

}// end file copy

if($insertedId = insert('users',$_POST)) {
  echo "User successfully added on Id #$insertedId";
} else {
  echo ":( FAILED";
}
